library(data.table)
library(ggplot2)
library(lfe)
library(stringr)
library(scales)
library(Hmisc)



##Appendix B3
#B3 Panel A
full <- fread('../data/all_records07-17wcfm.csv') ## ENTIRE HMDA (not just approved loans)

records <- full[loan_type == 1 & loan_purpose %in% c(1,3) & property_type == 1 & owner_occupancy == 1 & lien_status == 1]
##Do the same plot for Application Volume
records[TYPE %in% c(40,41) ,type := 'S']
records[TYPE %in% 10:14    ,type := 'B']
records<-records[type %in% c('B','S')]
records[, conf.pct := loan_amount / conforming_limit]
records[, jumbo := as.integer(conf.pct>1.005)]

byYear<- records[,j = list(volume = sum(loan_amount,na.rm=T)/1e6),by=c('year','jumbo')]
byYear[, type := 'Conforming']
byYear[jumbo==1, type := 'Jumbo']

##volume of applications
ggplot(byYear) + geom_bar(aes(x=year,y=volume,group=type,fill = type),stat = 'identity',position = 'dodge')  + theme_bw() + scale_fill_brewer(palette = 'Set1') + theme(legend.position = 'none') + scale_y_continuous(label = comma) + xlab(NULL) + ylab(NULL) + scale_x_continuous(breaks = seq(2007,2017,by=2))
ggsave('application_volume.png',height=3,width=5,units = 'in')

##Average loan approval per year for jumbo and conforming 
records[action_taken %in% c(3,7), approval := 0]
records[action_taken %in% c(1,2), approval := 1]
approval_rate<- records[, .(approval=mean(approval,na.rm=T)),by=c('year','jumbo')]
approval_rate[, type := 'Conforming']
approval_rate[jumbo==1, type := 'Jumbo']

#B3 Panel B
approval2 <- approval_rate[!is.na(approval_rate$jumbo),]
ggplot(approval2) + geom_line(aes(x=year,y=approval,group=type,color=type)) + theme_bw() + theme(legend.position = 'none') + scale_color_brewer(palette = 'Set1') + ylab(NULL) + xlab(NULL) + scale_y_continuous(labels = function(x) {paste0(x*100,'%')}) + scale_x_continuous(breaks = seq(2007,2017,by=2))
ggsave('approval_line.png',height=3,width=5,units = 'in')
